package com.apes.fn.fico.reimbursement.model;

import com.apes.fico.masterdata.gl.model.AccountingChart;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 费用报销明细表
 * Created by lishikun on 2021/1/27 10:23
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ReimbursementItem.CONST_TABLE_NAME)
public class ReimbursementItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_reimbursement_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "费用报销明细表", padding = 8)
    @Comment("费用报销明细编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "reimbursement_id", referencedColumnName = "id")
    @Comment("费用报销")
    private Reimbursement reimbursement;
    //
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "claimant_id", referencedColumnName = "id")
    @Comment("报销人")
    private PartyRole partyRole;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cost_center_id", referencedColumnName = "id")
    @Comment("报销部门")
    private DutyCenter dutyCenter;  //报销部门

    @Comment("报销总金额")
    @Column(name = "reimbursement_total")
    private double reimbursementTotal;

    @Comment("借支金额")
    @Column(name = "expense_debit_total")
    private double expenseDebitTotal = 0;

    @Comment("待支付金额")
    @Column(name = "await_pay_amount")
    private double awaitPayAmount = 0;

    @Comment("公司账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bankAccount_id", referencedColumnName = "id")
    private BankAccount bankAccount;

    @JoinColumn(name = "account_bank_name")
    @Comment("收款账户名称")
    private String accountBankName;

    @Comment("账户")
    @Column(name = "account_bank_number", length = 120)
    private String accountBankNumber;


    @Comment("开户行")
    @Column(name = "account_bank")
    private String accountBank;

    @Comment("支行")
    @Column(name = "subbranch", length = 200)
    private String subbranch;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Comment("文件路径")
    @Column(name = "fileu_rl ", columnDefinition = "CLOB")
    private String fileUrl;

    @Comment("备注")
    @Column(name = "remark", length = 250)
    private String remark;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fee_type_id", referencedColumnName = "id")
    @Comment("费用类型")
    private FeeType feeType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax;

    // TODO 进项税
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "input_tax_id", referencedColumnName = "id")
    @Comment("进项税")
    private Tax inputTax;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "accountingt_chart_id", referencedColumnName = "id")
    @Comment("会计科目")
    private AccountingChart accountingChart;

    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("发票类型")
    @JoinColumn(name = "invoice", referencedColumnName = "id")
    private Enumeration invoice; //发票类型

    @Comment("发票号码")
    @Column(name = "invoice_number", length = 120)
    private String invoiceNumber;

    @Comment("价税合计金额")
    @Column(name = "price_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double priceTax = 0;

    @Comment("进项税金")
    @Column(name = "input_tax_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double inputTaxAmount = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "xmh", referencedColumnName = "id")
    private Enumeration xmh;
    //
//    @Comment("费用报销明细")
//    @OneToMany(mappedBy = "reimbursementItem", cascade = CascadeType.ALL, orphanRemoval = true)
//    private List<ReimbursementTypeItem> reimbursementTypeItems = new ArrayList<>();
//
//    public List<ReimbursementTypeItem> getReimbursementTypeItems() {
//        for (ReimbursementTypeItem item : reimbursementTypeItems) {
//            item.setReimbursementItem(this);
//        }
//        return reimbursementTypeItems;
//    }
//
//    public void setReimbursementTypeItems(List<ReimbursementTypeItem> reimbursementTypeItems) {
//        if (reimbursementTypeItems == null) reimbursementTypeItems = new ArrayList<>();
//        this.reimbursementTypeItems.clear();
//        this.reimbursementTypeItems = reimbursementTypeItems;
//
//    }
    @Comment("借支明细")
    @OneToMany(mappedBy = "reimbursementItem", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ReimbursementDebit> reimbursementDebits = new ArrayList<>();

    public List<ReimbursementDebit> getReimbursementDebits() {
        for (ReimbursementDebit debit : reimbursementDebits) {
            debit.setReimbursementItem(this);
        }
        return reimbursementDebits;
    }

    public void setReimbursementDebits(List<ReimbursementDebit> reimbursementDebits) {
        if (reimbursementDebits == null) reimbursementDebits = new ArrayList<>();
        this.reimbursementDebits.clear();
        this.reimbursementDebits = reimbursementDebits;

    }
}
